package com.yao.component.pictureselector.adapter;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yao.component.pictureselector.R;
import com.yao.component.pictureselector.VideoBrowseActivity;
import com.yao.component.pictureselector.bean.VideoModel;
import com.yao.component.pictureselector.util.CommonUtil;
import com.yao.component.pictureselector.widget.AlertMsgDialog;
import com.yao.component.pictureselector.widget.ViewHolder;

import java.util.List;

public class VideoGrideAdapter extends CommonAdapter<VideoModel>
{


   private onVideoClickListener mListener;
   private int diaplayWidth;
   private boolean isSingle;
   public VideoGrideAdapter(Context context, List<VideoModel> mDatas, int itemLayoutId, boolean isSingle)
   {
       super(context, mDatas, itemLayoutId);
       this.isSingle=isSingle;
       diaplayWidth= (com.yao.component.pictureselector.CommonUtil.getDisplayWidth(context))/2;
//		this.mDirPath = dirPath;
   }
   public void setOnPictureClickListener(onVideoClickListener listener){
       this.mListener=listener;
   }
   @Override
   public void convert(final ViewHolder helper, final VideoModel item)
   {
       //设置no_pic
       helper.setImageResource(R.id.id_item_image, R.drawable.pictures_no);
       //设置no_selected
//				helper.setImageResource(R.id.id_item_select,
//						R.drawable.picture_unselected);
       //设置图片
       helper.setImageByUrl(R.id.id_item_image, item.getPath());
       final ImageView mImageView = helper.getView(R.id.id_item_image);
       final ImageView mSelect = helper.getView(R.id.id_item_select);
       TextView mTitle=helper.getView(R.id.title);
       TextView mDuration=helper.getView(R.id.duration);
       TextView mSize=helper.getView(R.id.size);
       mTitle.setText(item.getTitle());
       mDuration.setText(CommonUtil.millis2Time(item.getDuration()));
       mSize.setText(CommonUtil.formatFileSize(item.getSize()));
       if (isSingle){
           mSelect.setVisibility(View.GONE);
       }else {
           mSelect.setVisibility(View.VISIBLE);
       }
       helper.getView(R.id.rel_lay) .setLayoutParams(new RelativeLayout.LayoutParams(diaplayWidth,diaplayWidth));
       mImageView.setColorFilter(null);
       //设置ImageView的点击事件
       mSelect.setOnClickListener(new OnClickListener()
       {
           //选择，则将图片变暗，反之则反之
           @Override
           public void onClick(View v)
           {

               // 已经选择过该图片
               if (VideoBrowseActivity.mSelectedVideo.contains( item.getPath()))
               {

                   mSelect.setBackgroundResource(R.drawable.circular_uncheck);
                   if (mListener!=null){
                       mListener.onRemovePicture(item.getPath());
                   }
               } else
               // 未选择该图片
               {
                   if (VideoBrowseActivity.mSelectedVideo.size()>=VideoBrowseActivity.maxSelectCount){
                       AlertMsgDialog dialog=new AlertMsgDialog(mContext,"你最多只能选择"+VideoBrowseActivity.maxSelectCount+"个视频","确定",null);
                       dialog.show();
                   }else {
                       mSelect.setBackgroundResource(R.drawable.circular_check);
                       if (mListener!=null){
                           mListener.onAddPicture(item.getPath());
                       }
                   }

               }

           }
       });

       /**
        * 已经选择过的图片，显示出选择过的效果
        */
       if (VideoBrowseActivity.mSelectedVideo.contains(item))
       {
           mSelect.setBackgroundResource(R.drawable.circular_check);
       }else {
           mSelect.setBackgroundResource(R.drawable.circular_uncheck);
       }

   }
   public interface onVideoClickListener {
       public void onAddPicture(String path);
       public void onRemovePicture(String path);
   }
}
